package com.zcp.month2.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zcp.month2.R;
import com.zcp.month2.jiekou.CallBack2;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/1.
 */

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyViewHolder> {
    ArrayList<String> names;
    LayoutInflater inflater;
    CallBack2 callBack2;
    int a=0;
    View vv;

    public LeftAdapter(Context context, ArrayList<String> names) {
        this.inflater=LayoutInflater.from(context);
        this.names=names;
    }

    public void setListener(CallBack2 callBack2)
    {
        this.callBack2=callBack2;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = inflater.inflate(R.layout.left_item, parent, false);

        final MyViewHolder mv = new MyViewHolder(view);

        mv.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = mv.getAdapterPosition();
                callBack2.getX(position);
                if(a==0)
                {
                    vv=view;
                    a++;
                }
                if(vv!=v)
                {
                    vv.setBackgroundResource(R.color.colorBg);
                    vv=view;
                    view.setBackgroundColor(Color.WHITE);
                }


            }
        });
        return mv;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(names.get(position));

    }

    @Override
    public int getItemCount() {
        return names.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        private final TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
